package cn.ctodb.mall.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.mall.service.DeliveryGoodsService;
import cn.ctodb.mall.web.rest.util.HeaderUtil;
import cn.ctodb.mall.service.dto.DeliveryGoodsDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing DeliveryGoods.
 */
@RestController
@RequestMapping("/api")
public class DeliveryGoodsResource {

    private final Logger log = LoggerFactory.getLogger(DeliveryGoodsResource.class);
        
    @Inject
    private DeliveryGoodsService deliveryGoodsService;

    /**
     * POST  /delivery-goods : Create a new deliveryGoods.
     *
     * @param deliveryGoodsDTO the deliveryGoodsDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new deliveryGoodsDTO, or with status 400 (Bad Request) if the deliveryGoods has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/delivery-goods")
    @Timed
    public ResponseEntity<DeliveryGoodsDTO> createDeliveryGoods(@RequestBody DeliveryGoodsDTO deliveryGoodsDTO) throws URISyntaxException {
        log.debug("REST request to save DeliveryGoods : {}", deliveryGoodsDTO);
        if (deliveryGoodsDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("deliveryGoods", "idexists", "A new deliveryGoods cannot already have an ID")).body(null);
        }
        DeliveryGoodsDTO result = deliveryGoodsService.save(deliveryGoodsDTO);
        return ResponseEntity.created(new URI("/api/delivery-goods/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("deliveryGoods", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /delivery-goods : Updates an existing deliveryGoods.
     *
     * @param deliveryGoodsDTO the deliveryGoodsDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated deliveryGoodsDTO,
     * or with status 400 (Bad Request) if the deliveryGoodsDTO is not valid,
     * or with status 500 (Internal Server Error) if the deliveryGoodsDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/delivery-goods")
    @Timed
    public ResponseEntity<DeliveryGoodsDTO> updateDeliveryGoods(@RequestBody DeliveryGoodsDTO deliveryGoodsDTO) throws URISyntaxException {
        log.debug("REST request to update DeliveryGoods : {}", deliveryGoodsDTO);
        if (deliveryGoodsDTO.getId() == null) {
            return createDeliveryGoods(deliveryGoodsDTO);
        }
        DeliveryGoodsDTO result = deliveryGoodsService.save(deliveryGoodsDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("deliveryGoods", deliveryGoodsDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /delivery-goods : get all the deliveryGoods.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of deliveryGoods in body
     */
    @GetMapping("/delivery-goods")
    @Timed
    public List<DeliveryGoodsDTO> getAllDeliveryGoods() {
        log.debug("REST request to get all DeliveryGoods");
        return deliveryGoodsService.findAll();
    }

    /**
     * GET  /delivery-goods/:id : get the "id" deliveryGoods.
     *
     * @param id the id of the deliveryGoodsDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the deliveryGoodsDTO, or with status 404 (Not Found)
     */
    @GetMapping("/delivery-goods/{id}")
    @Timed
    public ResponseEntity<DeliveryGoodsDTO> getDeliveryGoods(@PathVariable Long id) {
        log.debug("REST request to get DeliveryGoods : {}", id);
        DeliveryGoodsDTO deliveryGoodsDTO = deliveryGoodsService.findOne(id);
        return Optional.ofNullable(deliveryGoodsDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /delivery-goods/:id : delete the "id" deliveryGoods.
     *
     * @param id the id of the deliveryGoodsDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/delivery-goods/{id}")
    @Timed
    public ResponseEntity<Void> deleteDeliveryGoods(@PathVariable Long id) {
        log.debug("REST request to delete DeliveryGoods : {}", id);
        deliveryGoodsService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("deliveryGoods", id.toString())).build();
    }

    /**
     * SEARCH  /_search/delivery-goods?query=:query : search for the deliveryGoods corresponding
     * to the query.
     *
     * @param query the query of the deliveryGoods search 
     * @return the result of the search
     */
    @GetMapping("/_search/delivery-goods")
    @Timed
    public List<DeliveryGoodsDTO> searchDeliveryGoods(@RequestParam String query) {
        log.debug("REST request to search DeliveryGoods for query {}", query);
        return deliveryGoodsService.search(query);
    }


}
